package com.czf.personal.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czf.common.entity.FilePathInfo;
import com.czf.common.mapper.CreditUserMapper;
import com.czf.common.mapper.FilePathInfoMapper;
import com.czf.common.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author chenzhanfan
 * @Date 2022/3/20
 * @Description
 */
@Service
@Slf4j
//@PropertySource(value = "classpath:application.yml") //指定配置文件的位置
public class FileUploadServiceImpl {

    @Value("${personal.fileAddress}")
    private String address;

    @Autowired
    private CreditUserMapper creditUserMapper;

    @Autowired
    private FilePathInfoMapper filePathInfoMapper;


    public Object upload(MultipartFile multipartFile, Long userId, String purpose, String remarks, Boolean personalOrEnterprise) {
        log.info("文件上传开始");
        log.info("文件保存地址 = {}", address);
        log.info("文件{}", multipartFile.getOriginalFilename());

        // 文件路径
        String fileSort = "credit/personal/";
        // 当天时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String today = format.format(new Date());
        // 文件存放路径
        String filePath = address + fileSort + today + "/" + userId + "/";
        log.info("文件存放路径={}", filePath);
        if (!multipartFile.isEmpty()) {
            try {
                //上传的文件需要保存的路径和文件名称，路径需要存在，否则报错
                File file = new File(filePath);
                file.mkdirs();
                File f = new File(filePath + multipartFile.getOriginalFilename());
                FileOutputStream stream = new FileOutputStream(f);
                stream.write(multipartFile.getBytes());
                stream.close();
            } catch (Exception e) {
                e.printStackTrace();
                return "上传失败";
            }
        } else {
            return "请上传文件";
        }
        FilePathInfo filePathInfo = new FilePathInfo();
        filePathInfo.setFileId(CommonUtils.uuid());
        filePathInfo.setUserId(userId);
        filePathInfo.setCreateTime(CommonUtils.timeStamp());
        filePathInfo.setPurpose(purpose);
        filePathInfo.setPath(filePath);
        filePathInfo.setFileName(multipartFile.getOriginalFilename());
        filePathInfo.setPersonalOrEnterprise(personalOrEnterprise);
        filePathInfo.setRemarks(remarks);
        filePathInfo.setStatus("未审核");
        int insert = filePathInfoMapper.insert(filePathInfo);
        if (insert == 1) {
            return filePathInfo.getFileId();
        }
        return null;
    }

    public List<FilePathInfo> getRecord(Long userId) {
        QueryWrapper<FilePathInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.orderByDesc("create_time");
        wrapper.last("limit 8");
        List<FilePathInfo> filePathInfos = filePathInfoMapper.selectList(wrapper);

        return filePathInfos;
    }
}
